#include <iostream>
#include <sstream>
#include "mysql\mysql.h"
#include "Transcoding.h"

using namespace std;

MYSQL mysql, *sock;

void test() 
{
	const char * host = "";
	const char * user = "";
	const char * passwd = "";
	const char * db = "";
	unsigned int port = 3306;
	const char * unix_socket = NULL;
	unsigned long client_flag = 0;

	const char * i_query = "select * from t_*";
	MYSQL_RES * result;
	MYSQL_ROW row;

	mysql_init(&mysql);

	//连接MySQL
	if ((sock = mysql_real_connect(&mysql, host, user, passwd, db, port, unix_socket, client_flag)) == NULL)
	{
		cout << Transcoding::UTF8ToGBK("连接失败: \n");
		cout << mysql_error(&mysql);
		exit(1);
	}
	else
		cout << Transcoding::UTF8ToGBK("连接成功 \n");

	//windows解决中文乱码
	if (mysql_query(&mysql, "SET NAMES GB2312"))
		cout << Transcoding::UTF8ToGBK("查询失败！\n");

	//如果连接成功，则开始查询
	if (mysql_query(&mysql, i_query) != 0)
	{
		cout << Transcoding::UTF8ToGBK("查询失败！\n");
		exit(1);
	}

	//保存查询的结果
	if ((result = mysql_store_result(&mysql)) == NULL)
	{
		cout << Transcoding::UTF8ToGBK("保存结果集失败！\n");
		exit(1);
	}

	//读取结果集中的数据，返回的是下一行。因为保存结果集时，当前的游标在第一行【之前】
	while ((row = mysql_fetch_row(result)) != NULL)
	{
		cout << row[0] << "\t" << row[1] << endl;
	}

	mysql_free_result(result);
	mysql_close(sock);
}

//int main(int argc, char *argv[])
//{
//	test();
//
//	system("pause");
//	return 0;
//}